* This file creates Figures F1, F2, F3 and Table F1

use "$inputdata/het_fulldiag_data.dta", clear

* Put effects in percentages
replace wt1 = -wt1


replace diff_cost30 = diff_cost30/1000
replace predmort = predmort*100
replace diff_death30 = diff_death30*100
replace diff_admit = diff_admit*100
replace diff_d_death = diff_d_death*100
format %9.0fc diff*

set more off

* Make Figure F1
*****************

gen cf240 = 1 - wt5
label var predmort "Predicted mortality (%)"
twoway scatter cf240 predmort, mcolor(black) || lfit cf240 predmort, lcolor(black) graphregion(color(white)) ytit("Proportion waiting beyond the threshold (%)") legend(off)

graph export "$results/FigF1.pdf",as(pdf) replace

* Make Figure F2
********************

* Panel A
label var predmort "Predicted mortality (%)"
label var wt1 "Diagnosis"
twoway scatter wt1 predmort, mcolor(black) || lfit wt1 predmort, lcolor(black) graphregion(color(white)) ytit("Waiting times reduction (mins)") legend(off)

graph export "$results/FigF2a.pdf",as(pdf) replace

* Panel B
label var diff_admit "Diagnosis"
twoway scatter diff_admit predmort, mcolor(black)  || lfit diff_admit predmort , lcolor(black) graphregion(color(white))  ytit("Increase in admissions (ppts)") legend(off)

graph export "$results/FigF2b.pdf",as(pdf) replace

*Panel C
label var diff_death30 "Observed mortality"
scatter diff_d_death diff_death30 predmort, msymbol(T 0) mcolor(red black)  || lfit diff_d_death predmort, lpattern(dash) lcolor(red)  || lfit diff_death30 predmort, lcolor(black) graphregion(color(white)) ytit("Mortality reduction (ppts)") legend(order(1 2) pos(5) ring(0))

graph export "$results/FigF2c.pdf",as(pdf) replace

* Check these slopes are significant
reg wt1 predmort

reg diff_admit predmort

reg diff_death30 predmort

*********************************************************
* Regression outputs - columns (1) to (3) for Table F1.
*********************************************************

reg diff_death30 wt1
outreg2 using "$results/TableF1.xls", replace

reg diff_death30 diff_admit
outreg2 using "$results/TableF1.xls", append

reg diff_death30 wt1 diff_admit
outreg2 using "$results/TableF1.xls", append


*** Crowding analysis
***********************

use "$inputdata/het_dist50maj_data.dta", clear

replace wt1 = -wt1

replace diff_cost30 = diff_cost30/1000
replace diff_death30 = diff_death30*100
replace diff_admit = diff_admit*100
replace diff_d_death = diff_d_death*100
format %9.0fc diff*


** Make Figure 3
******************

* Panel A
label var wt1 "Crowding-severity group"
twoway scatter wt1 bin, mcolor(black) || lfit wt1 bin, lcolor(black) graphregion(color(white)) ytit("Waiting times reduction (mins)") legend(off)
graph export "$results/FigF3a.pdf",as(pdf) replace

* Panel B
label var diff_admit "Crowding-severity group"
twoway scatter diff_admit bin, mcolor(black) || lfit diff_admit bin, lcolor(black)  graphregion(color(white)) ytit("Increase in admissions (ppts)") legend(off)
graph export "$results/FigF3b.pdf",as(pdf) replace

* Panel C
scatter diff_d_death diff_death bin, msymbol(T 0) mcolor(red black)  || lfit diff_d_death bin, lpattern(dash) lcolor(red)  || lfit diff_death bin, lcolor(black) graphregion(color(white)) ytit("Mortality reduction (ppts)") legend(order(1 2) pos(7) ring(0))
graph export "$results/FigF3c.pdf",as(pdf) replace

** To check the slopes
reg wt1 bin

reg diff_admit bin

reg diff_death bin


*********************************************************
* Regression outputs - columns (4) to (6) for Table F1.
*********************************************************
reg diff_death wt1 
outreg2 using "$results/TableF1.xls", append

reg diff_death diff_admit
outreg2 using "$results/TableF1.xls", append

reg diff_death wt1 diff_admit
outreg2 using "$results/TableF1.xls", append


